/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.ArrayList;
import java.util.Collection;
import javax.swing.ComboBoxModel;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author minaev
 */
public class ArrayListModel<T> extends ArrayList<T> implements ComboBoxModel<T> {

    private ArrayList<ListDataListener> listeners = new ArrayList<ListDataListener>();
    Object selected_item = null;
    
    @Override
    public boolean add(T e) {
        boolean rt = super.add(e);
        added(0, this.getSize());
        return rt;
    }

    @Override
    public void add(int index, T element) {
        super.add(index, element);
        added(0, this.getSize());
    }

    @Override
    public boolean addAll(Collection<? extends T> c) {
        boolean rt = super.addAll(c);
        added(0, this.getSize());
        return rt;
    }

    @Override
    public boolean addAll(int index, Collection<? extends T> c) {
        boolean rt = super.addAll(index, c);
        added(0, this.getSize());
        return rt;
    }

    @Override
    public void clear() {
        int end  = this.getSize();
        super.clear();
        removed(0, end);
    }

    @Override
    public boolean remove(Object o) {
        int end = this.getSize();
        boolean rt = super.remove(o);
        removed(0, end);
        return rt;
    }

    @Override
    public T remove(int index) {
        int end = this.getSize();
        T t = super.remove(index);
        removed(0, end);
        return t;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        int end = this.getSize();
        boolean rt = super.removeAll(c);
        removed(0, end);
        return super.removeAll(c);
    }

    @Override
    protected void removeRange(int fromIndex, int toIndex) {
        int end = this.getSize();
        super.removeRange(fromIndex, toIndex);
        removed(0, end);
    }

    @Override
    public T set(int index, T element) {
        T rt = super.set(index, element);
        changed(index, index);
        return rt;
    }

    @Override
    public void trimToSize() {
        int end = this.getSize();
        super.trimToSize();
        removed(0, end);
    }
    
    
    
    
    
    //ListModel
    @Override
    public int getSize() {
        return this.size();
    }

    @Override
    public T getElementAt(int index) {
        return this.get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {
        listeners.add(l);
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
        listeners.remove(l);
    }

    private void added(int start, int end) {
        for (int i = 0; i < listeners.size(); i++) {
            listeners.get(i).intervalAdded(
                    new ListDataEvent(this,
                    ListDataEvent.INTERVAL_ADDED,
                    start,
                    end));
        }
    }

    private void removed(int start, int end) {
        for (int i = 0; i < listeners.size(); i++) {
            listeners.get(i).intervalRemoved(
                    new ListDataEvent(this,
                    ListDataEvent.INTERVAL_REMOVED,
                    start,
                    end));
        }
    }

    private void changed(int start, int end) {
        for (int i = 0; i < listeners.size(); i++) {
            listeners.get(i).contentsChanged(
                    new ListDataEvent(this,
                    ListDataEvent.CONTENTS_CHANGED,
                    start,
                    end));
        }
    }

    @Override
    public void setSelectedItem(Object anItem) {
        selected_item = anItem;
    }

    @Override
    public Object getSelectedItem() {
        return selected_item;
    }
}
